/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project.jpdftools.managers;

import java.text.MessageFormat;
import java.util.Date;
import java.util.logging.FileHandler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import project.jpdftools.toolkit.Toolkit;

/**
 *
 * @author Mido
 */
public class LoggerManager {

    private static final Logger logger = Logger.getLogger("EventLog");  // Logger object that is used to log all the events

    /**
     * this part will be executed while initializing the program
     */
    static {
        try {
            FileHandler fh = new FileHandler("EventLog.txt", true);
            // create a formatter to format the message written to the file
            fh.setFormatter(new java.util.logging.Formatter() {
                @Override
                public String format(LogRecord record) {
                    StringBuilder buf = new StringBuilder(1000);
                    Date date = new Date();
                    date.setTime(record.getMillis());

                    buf.append(new MessageFormat("{0,date} {0,time}").format(new Object[]{date}, new StringBuffer(), null));
                    buf.append(" (");
                    buf.append(record.getLevel());
                    buf.append(") ");
                    buf.append(formatMessage(record));
                    buf.append(Toolkit.getLineSeparator());
                    return buf.toString();
                }
            });
            logger.addHandler(fh);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Returns the logger object
     *
     * @return
     */
    public static Logger getLogger() {
        return logger;
    }
}
